#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<string>


int main(int argc,char** argv)
{
    if(argc != 3)
    {   
        std::cerr << "usage:" << argv[0] << " serverip serverport"<<std::endl;
        exit(1);
    }
    int sockfd =  socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        std::cerr << "socket error" <<std::endl;
        exit(2);
    }
    struct sockaddr_in perr;
    memset(&perr,0,sizeof(perr));
    perr.sin_family = AF_INET;
    perr.sin_addr.s_addr = inet_addr(argv[1]);
    perr.sin_port  =  htons(std::stoi(argv[2]));
    socklen_t len  = sizeof(perr);
    while(true)
    {
        std::string input;
        std::cout << "please input:";
        getline(std::cin,input);
        sendto(sockfd,input.c_str(),input.size(),0,(struct sockaddr*)&perr,len);
        char buffer[1024];
        ssize_t n =  recvfrom(sockfd,buffer,sizeof(buffer) - 1,0,(struct sockaddr*)&perr,&len);
        buffer[n] = 0;
        std::cout << buffer << std::endl;
    }

    return 0;
}